﻿using MySystemSetup.BusinessDomain;
using MySystemSetup.IEntrepreneurInfo;
using MySystemSetup.IEntrepreneurInfo.Dto;
using MySystemSetup.RBACDomain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TodoApp.Dto;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace MySystemSetup.EntrepreneurInfo
{
    public class EntrepreneurAppService: ApplicationService,IEntrepreneurAppService
    {



        public readonly IRepository<RoleInfo, int> _roleRepository;
        public readonly IRepository<UserInfo, string> _userRepoistory;
        public readonly IRepository<UserRole, int> _userRoleRepository;
        public readonly IRepository<Entrepreneur, string> _entrepreneurRe;
        public readonly IRepository<RolePermission, int> _rolePerResRepository;
        public readonly IRepository<UserInfo, string> _userRepository;
        public readonly IRepository<FieldInfo, int> _fieldRepository;
        public EntrepreneurAppService(IRepository<RoleInfo, int> roleRepository, IRepository<UserRole, int> userRoleRepository, IRepository<RolePermission, int> rolePerResRepository, IRepository<GiveInfo, int> giveRepository, IRepository<UserInfo, string> userRepoistory, IRepository<FieldInfo, int> fieldRepository, IRepository<Entrepreneur, string> entrepreneurRe)
        {
            _roleRepository = roleRepository;

            _userRoleRepository = userRoleRepository;
            _rolePerResRepository = rolePerResRepository;

            _userRepoistory = userRepoistory;
            _fieldRepository = fieldRepository;
            _entrepreneurRe = entrepreneurRe;
        }

      /// <summary>
      /// 分页显示羊村信息
      /// </summary>
      /// <param name="page">页序号</param>
      /// <param name="limit">页容量</param>
      /// <param name="getUId">负责人编号</param>
      /// <param name="eName">羊村名称</param>
      /// <param name="uId">用户编号</param>
      /// <returns>统一返回格式</returns>

        public async Task<SzrApiPageResult<EntrepreneurShowDto>> GetEntrepreneurList(int page, int limit, string? getUId, string? eName, string uId)
        {



            List<Entrepreneur>? giveRes = await _entrepreneurRe.GetListAsync();
            var elRes = giveRes.WhereIf(getUId != null, c => c.PrincipalId == getUId).
                   WhereIf(eName != null, c => c.EName.Contains(eName));

            List<RoleInfo>? ress = await _roleRepository.GetListAsync();
            var userRole = await _userRoleRepository.GetListAsync(c => c.UId == uId);
            var rolePer = await _rolePerResRepository.GetListAsync();


            var res = userRole.Join(rolePer, a => a.RoleId, b => b.Id,
                (a, b) => new { a, b });

            var rolePerRes = res.Join(ress, a => a.b.RoleId,
                b => b.Id, (a, b) => new { a, b })
                .OrderBy(c => c.b.RoleGrade).FirstOrDefault();



            var userList = await _userRepoistory.GetListAsync();


          //  var list = Enum.GetValues<GiveType>().Select(c => new
           

            return new SzrApiPageResult<EntrepreneurShowDto>()
            {
                code = 0,
                data = elRes.Join(userList, a => a.PrincipalId, b => b.Id,
                (a, b) => new { a, b }).
                Select(c => new EntrepreneurShowDto()
                {
                    Id = c.a.Id,
                    EName = c.a.EName,
                    EAddress= c.a.EAddress,
                    EDate= c.a.EDate,
                    EntrepreneurName=c.a.EntrepreneurName,
                    DistrictId=c.a.DistrictId,
                    PrincipalId=c.b.UserName,
                    EState= c.a.EState,
                    SheepSize= c.a.SheepSize,
                    EDId=c.a.EDId,
                    ButtonPermiss= rolePerRes.a.b.ButtonPermiss



                }).Skip((page - 1) * limit).Take(limit).ToList(),
                count = elRes.Count(),
                page = (int)Math.Ceiling(elRes.Count() * 1.0 / limit)
            };





        }


        /// <summary>
        /// 添加羊场
        /// </summary>
        /// <param name="dto">要添加的集合</param>
        /// <returns>统一返回格式</returns>
        public  async Task<SzrApiResult<EntrepreneurAddDto>> AddEntrepreneurAsync(EntrepreneurAddDto dto)
        {



            dto.Id= Guid.NewGuid().ToString().Replace("-", "");


            var res = ObjectMapper.Map<EntrepreneurAddDto, Entrepreneur>(dto);





            await _entrepreneurRe.InsertAsync(res);


            return new SzrApiResult<EntrepreneurAddDto>
            {

                code = 200,
                msg = "添加成功"
            };


        }
    }
}
